package com.hgd.security.evp.biz.rest;

import com.alibaba.fastjson.JSONObject;
import com.hgd.security.evp.auth.entity.CustomUserDetails;
import com.hgd.security.evp.biz.biz.EvpUserBiz;
import com.hgd.security.evp.biz.entity.EvpUser;
import com.hgd.security.evp.biz.vo.EvpUserVo;
import com.hgd.security.evp.common.constances.CustomHttpStatus;
import com.hgd.security.evp.common.util.EmptyUtil;
import com.hgd.security.evp.common.util.ListResult;
import com.hgd.security.evp.common.util.ObjectResultResponse;
import com.hgd.security.evp.common.util.TableResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("eUser")
@Api(tags = "用户管理")
@Slf4j
public class EUserController {

    @Autowired
    private EvpUserBiz eUserBiz;

    /**
     * 返回值包含--用户ID(userId)，用户帐号(eAccount)，用户密码(ePassword)，用户名(userName)，部门ID(deptId)
     *
     * @param eAccount
     * @return
     */
    @GetMapping("/eAccount")
    @ApiOperation("按用户帐号查询用户")
    public EvpUser getUserByEAccount(@RequestParam(value = "eAccount") @ApiParam(value = "用户帐号") String eAccount) {
        return eUserBiz.getUserByEAccount(eAccount);
    }

    @GetMapping("/list/{groupId}/users")
    @ApiOperation("获取某组下的用户")
    public TableResultResponse<JSONObject> getUserOfGroup(
        @PathVariable(value = "groupId") @ApiParam("组ID") Integer groupId) {
        return eUserBiz.getUserOfGroup(groupId);
    }

    @GetMapping("/list")
    @ApiOperation("分页获取用户列表")
    public ListResult<EvpUserVo> getUserList(@RequestParam(value = "eAccount", defaultValue = "") String eAccount,
        @RequestParam(value = "phone", defaultValue = "") String phone,
        @RequestParam(value = "userName", defaultValue = "") String userName,
        @RequestParam(value = "evpAccount", defaultValue = "") String evpAccount,
        @RequestParam(value = "page", defaultValue = "1") Integer page,
        @RequestParam(value = "rows", defaultValue = "10") Integer rows) {
        EvpUser user = new EvpUser();
        user.setEvpAccount(eAccount);
        user.setPhone(phone);
        user.setUserName(userName);
        user.setEvpAccount(evpAccount);

        return eUserBiz.getUserLiset(user, page, rows);
    }

    @GetMapping("/list/combobox")
    @ApiOperation("分页获取用户列表")
    public List<EvpUserVo> getUserListCombobox(@RequestParam(value = "eAccount", defaultValue = "") String eAccount,
        @RequestParam(value = "phone", defaultValue = "") String phone,
        @RequestParam(value = "userName", defaultValue = "") String userName,
        @RequestParam(value = "evpAccount", defaultValue = "") String evpAccount,
        @RequestParam(value = "page", defaultValue = "1") Integer page,
        @RequestParam(value = "rows", defaultValue = "10") Integer rows) {
        EvpUser user = new EvpUser();
        user.setEvpAccount(eAccount);
        user.setPhone(phone);
        user.setUserName(userName);
        user.setEvpAccount(evpAccount);

        ListResult<EvpUserVo> userLiset = null;
        userLiset = eUserBiz.getUserLiset(user, page, rows);
        if (EmptyUtil.isNotEmpty(userLiset)) {
            return userLiset.getRows();
        }
        return new ArrayList<>();
    }

    @PostMapping("/instance")
    @ApiOperation("添加单个对象")
    public ObjectResultResponse<Void> addUser(@RequestBody @ApiParam("待添加对象") EvpUser user,
        BindingResult bindingResult) {
        ObjectResultResponse<Void> restResult = new ObjectResultResponse<>();

        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getAllErrors().get(0).getDefaultMessage();
            restResult.setStatus(CustomHttpStatus.COMMON_ERROR);
            restResult.setMessage(defaultMessage);
            return restResult;
        }

        return eUserBiz.addUser(user);
    }

    @PutMapping("/instance")
    @ApiOperation("更新单个对象")
    public ObjectResultResponse<Void> updateUser(@RequestBody @ApiParam("待更新对象") EvpUser user,
        BindingResult bindingResult) {
        ObjectResultResponse<Void> restResult = new ObjectResultResponse<>();

        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getAllErrors().get(0).getDefaultMessage();
            restResult.setStatus(CustomHttpStatus.COMMON_ERROR);
            restResult.setMessage(defaultMessage);
            return restResult;
        }
        return eUserBiz.updateUser(user);
    }

    @DeleteMapping("/instance/{id}")
    @ApiOperation("添加单个对象")
    public ObjectResultResponse<Void> delUser(@PathVariable(value = "id") Integer id) {
        ObjectResultResponse<Void> restResult = new ObjectResultResponse<>();

        return eUserBiz.delUser(id);
    }

    @GetMapping("/current")
    @ApiOperation("获取当前用户")
    public CustomUserDetails getCurrentUser(@AuthenticationPrincipal CustomUserDetails userDetails) {
        return userDetails;
    }
}